import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from web.split.readData import read_data

class operation:
    def driver(self,url ,driver):
        driver.get(url)
        time.sleep(3)
        driver.maximize_window()
        time.sleep(2)
    def login(self,loginname,pwd,driver):
        driver.find_element(By.ID, "username").send_keys(loginname)
        driver.find_element(By.ID, "password").send_keys(pwd)
        time.sleep(2)
        # 点击登录按钮
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(3)
        return driver.title
    def register(self,loginname,pwd,repwd,driver):
        # 寻找注册表单
        driver.find_element(By.ID, "username").send_keys(loginname)
        if pwd is not None:
            driver.find_element(By.ID, "password").send_keys(pwd)
        if repwd is not None:
            driver.find_element(By.ID, "repassword").send_keys(repwd)
        time.sleep(2)
        # 点击注册按钮
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(3)
        return driver.title
    def bankcard(self,bankNmae,cardtype,bankCard,driver):
        driver.find_element(By.PARTIAL_LINK_TEXT, '个人中心').click()
        time.sleep(3)
        # 寻找银行卡管理
        driver.find_element(By.PARTIAL_LINK_TEXT, '银行卡管理').click()
        time.sleep(3)
        result = driver.find_elements(By.XPATH, '//*[@class="col-sm-12"]')
        # 寻找新增按钮
        driver.find_element(By.ID, "bankCard_add_modal_btn").click()
        time.sleep(3)
        # 新增银行卡
        driver.find_element(By.ID, "cardBank_add_input").send_keys(bankNmae)
        driver.find_element(By.XPATH, '//label[@class="lyear-radio radio-inline"][{}]'.format(cardtype)).click()
        driver.find_element(By.ID, "cardNum_add_input").send_keys(bankCard)
        time.sleep(3)
        driver.find_element(By.ID, 'bankCard_save_btn').click()
        time.sleep(3)
        return len(result)+1

    def fund(self,pwd,driver):
        driver.find_element(By.XPATH, ' //*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a/i').click()
        time.sleep(3)
        driver.find_element(By.PARTIAL_LINK_TEXT, '零钱理财').click()
        time.sleep(3)
        # 投资
        driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(3)
        # 输入密码
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys(pwd)
        time.sleep(2)
        # 确认
        driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(5)
        result = driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div').text
        return result

    def loan(self,amount,term,driver):
        driver.find_element(By.PARTIAL_LINK_TEXT, '个人中心').click()
        time.sleep(3)
        # 寻找银行卡管理
        driver.find_element(By.PARTIAL_LINK_TEXT, '我的借贷').click()
        time.sleep(3)
        elements = driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[2]/div/button[6]').text
        result = ''
        for char in elements:
            if char.isdigit():
                result += char
        driver.find_element(By.PARTIAL_LINK_TEXT, '金融工具').click()
        time.sleep(3)
        driver.find_element(By.PARTIAL_LINK_TEXT, '安全网贷').click()
        time.sleep(3)
        # 贷款金额
        driver.find_element(By.ID, 'amount').send_keys(amount)
        time.sleep(2)
        driver.find_element(By.ID, 'term').send_keys(term)
        time.sleep(2)
        driver.find_element(By.ID, 'submit').click()
        time.sleep(2)
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)
        return int(result) + 1

    def quit(self,driver):
        driver.quit()
